package com.yunao.web.controller;

import com.yunao.commons.serachcore.core.bean.*;
import com.yunao.commons.serachcore.core.help.GroupField;
import com.yunao.commons.utils.ResultVOUtil;
import com.yunao.commons.vo.ResultVO;
import com.yunao.web.annotation.UserLoginToken;
import com.yunao.web.convert.QueryForm2Map;
import com.yunao.web.form.QueryForm;
import com.yunao.service.IsearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/search/analysis")
public class AnalysisController {
    @Autowired
    private IsearchService service;

    @UserLoginToken
    @PostMapping("/trand")
    public ResultVO trand(QueryForm queryForm) {
//		Map<String, String> params = RequestParamUtils.parseParams(request);
        Map<String, String> params = QueryForm2Map.analysisConvert(queryForm);
        params.put("group",GroupField.DAY.toString());
        Groups groups = service.group(params);
        return ResultVOUtil.success(groups);
    }
    @UserLoginToken
    @PostMapping("/emotion")
    public ResultVO emotion( QueryForm queryForm) {
//		Map<String, String> params = RequestParamUtils.parseParams(request);
        Map<String, String> params = QueryForm2Map.analysisConvert(queryForm);
        params.put("group",GroupField.EMOTION.toString());
        Groups groups = service.group(params);
        return ResultVOUtil.success(groups);
    }

    @UserLoginToken
    @PostMapping("/cluster")
    public ResultVO cluster(QueryForm queryForm) {
        Map<String, String> params = QueryForm2Map.analysisConvert(queryForm);
        params.put("group",GroupField.DAY.toString());
        params.put("max_group","20");
        params.put("message_type","正文");
        params.put("distance","0.5");
        Clusters clusters = service.cluster(params);
        return ResultVOUtil.success(clusters);
    }
    @UserLoginToken
    @PostMapping("/wordrelation")
    public ResultVO wordrelation(QueryForm queryForm) {
        Map<String, String> params = QueryForm2Map.analysisConvert(queryForm);
        Wordrelations wordrelations = service.wordrelation(params);
        return ResultVOUtil.success(wordrelations);
    }
    @UserLoginToken
    @PostMapping("/wordcloud")
    public ResultVO wordcloud(QueryForm queryForm) {
        Map<String, String> params = QueryForm2Map.analysisConvert(queryForm);
        Wordclouds wordclouds = service.wordcloud(params);
        return ResultVOUtil.success(wordclouds);
    }

    @UserLoginToken
    @PostMapping("/domain")
    public ResultVO domain(QueryForm queryForm) {
        Map<String, String> params = QueryForm2Map.analysisConvert(queryForm);
        params.put("group",GroupField.DOMAIN.toString());
        Groups groups = service.group(params);
        return ResultVOUtil.success(groups);
    }

    @UserLoginToken
    @PostMapping("/media")
    public ResultVO media(QueryForm queryForm) {
        Map<String, String> params = QueryForm2Map.analysisConvert(queryForm);
        params.put("group",GroupField.MEDIA_TYPE.toString());
        Groups groups = service.group(params);
        return ResultVOUtil.success(groups);
    }

    @UserLoginToken
    @PostMapping("/medialevel")
    public ResultVO medialevel(QueryForm queryForm) {
        Map<String, String> params = QueryForm2Map.analysisConvert(queryForm);
        params.put("group",GroupField.DOMAIN_LEVEL.toString());
        Groups groups = service.group(params);
        return ResultVOUtil.success(groups);
    }
    @UserLoginToken
    @PostMapping("/author")
    public ResultVO author(QueryForm queryForm) {
        Map<String, String> params = QueryForm2Map.analysisConvert(queryForm);
        params.put("group",GroupField.AUTHOR.toString());
        Groups groups = service.group(params);
        return ResultVOUtil.success(groups);
    }

    @PostMapping("/location")
    public ResultVO location(QueryForm queryForm) {
        Map<String, String> params = QueryForm2Map.analysisConvert(queryForm);
        params.put("group",GroupField.LOCATION.toString());
        Groups groups = selectPro(service.group(params));
        return ResultVOUtil.success(groups);
    }

    @PostMapping("/countryHot")
    public ResultVO country(@RequestParam(name = "days",defaultValue = "3",required = false) String days) {
        Map<String, String> params = new HashMap<>();
        params.put("day",days);
        params.put("group",GroupField.LOCATION.toString());
        params.put("limit","1000");
        Groups groups = selectPro(service.group(params));
        return ResultVOUtil.success(groups);
    }



    private Groups selectPro(Groups groups){
        List<Group> list = new ArrayList<>(36);
        int total=0;
        for (Group group : groups.getData()) {
//            if (locallist.contains(group.getKey())){
            if (!group.getKey().contains(":")){
                list.add(group);
                total += group.getValue();
            }
        }
        Groups result = new Groups(total);
        for (Group group : list){
            result.addData(group);
        }
        return result;
    }



}
